package web.remoteAccess.action;

import com.opensymphony.xwork2.ActionSupport;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.AddressService;
import services.RemoteAccessService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: scamisay
 * Date: 10/22/12
 * Time: 4:57 PM
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/RemoteAccess")
public class RemoteAccessController extends ActionSupport {

    private RemoteAccessService remoteAccessService;
    private String auth;
    private String message;
    private Vendorcentral laboratory;
    private List<Countries> countries = new ArrayList<Countries>();
    private List<Regions> regions = new ArrayList<Regions>();
    private List<Cities> cities = new ArrayList<Cities>();
    private AddressService addressService;

    @Autowired
    public RemoteAccessController(RemoteAccessService remoteAccessService, AddressService addressService) {
        this.remoteAccessService = remoteAccessService;
        this.addressService = addressService;
        countries = this.addressService.getCountries();
    }

    @Action(value="getURL", results={
            @Result(name="success",location="/WEB-INF/jsp/RemoteAccess/newURL.jsp")
    })
    public String getURL(){
        auth = remoteAccessService.getNewURL().getHash();
        return SUCCESS;
    }

    @Action(value="getRemoteAccess", results={
            @Result(name="success",location="/WEB-INF/jsp/RemoteAccess/create.jsp") ,
            @Result(name="input",location="/WEB-INF/jsp/RemoteAccess/message.jsp")
    })
    public String getRemoteAccess(){
        RemoteAccess remoteAccess = remoteAccessService.getValidRemoteAccess(auth);

        if(remoteAccess == null || !remoteAccess.isActive()){
            message = "Invalid Hash";
            return INPUT;
        }

        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="/WEB-INF/jsp/RemoteAccess/create.jsp"),
            @Result(name="input",location="/WEB-INF/jsp/RemoteAccess/create.jsp")
    })
    public String save() {
        RemoteAccess remoteAccess = remoteAccessService.getValidRemoteAccess(auth);
        if(remoteAccess == null || !remoteAccess.isActive()){
            message = "Invalid Hash";
            return SUCCESS;
        }

        /*validate*/
        LaboratoryValidator lv = new LaboratoryValidator(laboratory);
        if(lv.hasErrors())
            return INPUT;

        /*save*/
        remoteAccessService.save(laboratory);

        return SUCCESS;
    }

    private void validateLaboratory(Vendorcentral laboratory){


    }

    public String getAuth() {
        return auth;
    }

    public void setAuth(String auth) {
        this.auth = auth;
    }

    public String getMessage() {
        return message;
    }

    public Vendorcentral getLaboratory() {
        return laboratory;
    }

    public void setLaboratory(Vendorcentral laboratory) {
        this.laboratory = laboratory;
    }

    public List<Countries> getCountries() {
        return countries;
    }

    public List<Regions> getRegions() {
        return regions;
    }

    public List<Cities> getCities() {
        return cities;
    }

    class LaboratoryValidator{

        private Vendorcentral laboratory;
        private boolean name;

        LaboratoryValidator(Vendorcentral laboratory) {
            this.laboratory = laboratory;
        }

        boolean hasErrors(){
            boolean hasErrors = false;
            name = laboratory.getName() == null || laboratory.getName().isEmpty();

            hasErrors &= name;
            return hasErrors;
        }

        public boolean isName() {
            return name;
        }

        public void setName(boolean name) {
            this.name = name;
        }
    }
}
